﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Globalization;
using System.Reflection;

namespace ProductKeyManager
{
    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public partial class Settings : Window
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Settings"/> class.
        /// </summary>
        public Settings()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Assembly assm = Assembly.GetExecutingAssembly();
            mLogFile.Text = assm.Location.Replace("ProductKeyManager.exe", MainWindow.logFile);
            #region Basic Tab
            this.chkSaveLastSearch.IsChecked = Properties.Settings.Default.SaveLastSearch;
            this.chkLockToolbars.IsChecked = Properties.Settings.Default.LockToolBars;
            this.chkEnableXsl.IsChecked = Properties.Settings.Default.EnableXslExport;

            // Only enable the Xsl Prompt if Xsl is enabled
            this.chkXslPrompt.IsEnabled = Properties.Settings.Default.EnableXslExport;
            this.chkXslPrompt.IsChecked = Properties.Settings.Default.PromptXsl;

            this.sldSearchMinimum.Value = Properties.Settings.Default.DefaultSearchLimit;
            #endregion

            #region DataLayer
            this.mConnectionString.Text = Properties.Settings.Default.ConnectionString;
            #endregion
        }

        #region Basic Tab
        private void chkEnableXsl_Checked(object sender, RoutedEventArgs e)
        {
            this.chkXslPrompt.IsEnabled = (bool)this.chkEnableXsl.IsChecked;
        }
        #endregion

        private void SettingsApply_Click(object sender, RoutedEventArgs e)
        {
            #region Basic Tab
            Properties.Settings.Default.SaveLastSearch = (bool)this.chkSaveLastSearch.IsChecked;
            Properties.Settings.Default.LockToolBars = (bool)this.chkLockToolbars.IsChecked;
            Properties.Settings.Default.EnableXslExport = (bool)this.chkEnableXsl.IsChecked;
            Properties.Settings.Default.PromptXsl = (bool)this.chkXslPrompt.IsChecked;
            Properties.Settings.Default.DefaultSearchLimit = this.sldSearchMinimum.Value;
            #endregion

            Properties.Settings.Default.Save();
        }

        private void SettingsCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void SettingsOk_Click(object sender, RoutedEventArgs e)
        {
            SettingsApply_Click(sender, e);
            this.Close();
        }

        private void sldSearchMinimum_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            this.lblCurrentSetting.Content = String.Format(CultureInfo.CurrentCulture, " {0}%", this.sldSearchMinimum.Value * 100);
        }
    }
}
